<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="省份" prop="provinceId">
            <el-select
              v-model="queryParams.provinceId"
              placeholder="选择省份"
              clearable
              @change="changeProvince"
              @clear="clearProvice"
            >
              <el-option
                v-for="dict in countryList"
                :key="dict.code"
                :label="dict.name"
                :value="dict.code"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-select
            v-model="queryParams.cityId"
            placeholder="选择市"
            clearable
            filterable
            style="margin-left: 20px"
            @change="changeCity"
            @clear="clearCity"
          >
            <el-option
              v-for="dict in cityList"
              :key="dict.code"
              :label="dict.name"
              :value="dict.code"
            />
          </el-select>
        </el-col>
        <el-col :span="8">
          <el-select
            v-model="queryParams.areaId"
            placeholder="选择区"
            clearable
            filterable
            style="margin-left: 20px"
          >
            <el-option
              v-for="dict in areaList"
              :key="dict.code"
              :label="dict.name"
              :value="dict.code"
            />
          </el-select>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">
          <el-form-item label="奖品筛选" prop="rewardId">
            <el-select
              v-model="queryParams.rewardId"
              placeholder="奖品筛选"
              clearable
              filterable
            >
              <!-- <el-option-group
                v-for="group in rewardList"
                :key="group.label"
                :label="group.label"
              > -->
                <!-- <el-option
                  v-for="item in group.options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                /> -->
              <!-- </el-option-group> -->
              <el-option
                  v-for="item in rewardList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="领取状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="领取状态"
              clearable
            >
              <el-option
                v-for="dict in rewardStatus"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="中奖时间" prop="time">
            <el-date-picker
              v-model="queryParams.time"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              size="small"
              @change="changeTime"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="搜索" prop="activityCode">
        <el-input
          v-model="queryParams.activityCode"
          placeholder="请输入活动码序列号"
          clearable
          style="width: 280px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          v-hasPermi="['system:role:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >批量奖品补发</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :show-search.sync="showSearch"
        @queryTable="getList(queryParams)"
      />
    </el-row>
    <el-table
      v-loading="loading"
      :data="recordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动码序列号" prop="activityCode" width="120" />
      <el-table-column
        label="奖品名称"
        prop="title"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="openid"
        prop="openid"
        :show-overflow-tooltip="true"
        width="290"
      />
      <el-table-column
        label="中奖区域"
        prop="provinceName"
        :show-overflow-tooltip="true"
        width="180"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.provinceName }} {{ scope.row.cityName }}
            {{ scope.row.areaName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="用户收货信息"
        prop="receieveInfo"
        :show-overflow-tooltip="true"
        width="280"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.receiveInfo != ''">
            地址：{{ JSON.parse(scope.row.receiveInfo).address }}
          </div>
          <div v-if="scope.row.receiveInfo != ''">
            电话：{{ JSON.parse(scope.row.receiveInfo).phone }}
          </div>
          <div v-if="scope.row.receiveInfo != ''">
            身份证：{{ JSON.parse(scope.row.receiveInfo).idCard }}
          </div>
          <div v-if="scope.row.receiveInfo != ''">
            姓名： {{ JSON.parse(scope.row.receiveInfo).receiveName }}
          </div>
          <div v-if="scope.row.receiveInfo != ''">
            城市:{{ JSON.parse(scope.row.receiveInfo).city }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="领取状态"
        prop="statusDesc"
        :show-overflow-tooltip="true"
        width="150"
      />

      <el-table-column
        label="中奖时间"
        align="center"
        prop="created"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.created) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="领取时间"
        align="center"
        prop="receiveTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-popconfirm
            confirm-button-text="确认"
            cancel-button-text="取消"
            icon-color="#626AEF"
            title="确定给当前用户补发奖品吗"
            @confirm="confirmEvent(scope.row)"
            @cancel="cancelEvent"
          >
            <template #reference>
              <el-button
                v-show="
                  scope.row.status == 2 &&
                  (scope.row.transferStatus == 2 ||
                    scope.row.transferStatus == 0)
                "
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.row)"
                >奖品补发</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList(queryParams)"
    />
    <!-- 提示框1 -->
    <el-dialog
      v-model="tipShow1"
      title="温馨提示"
      width="500"
      :visible.sync="open"
    >
      <span>请先选择正确的领取状态，只有已失效的用户可以补发奖品</span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelTip1">取消</el-button>
          <el-button type="primary" @click="confirmTip1"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 提示框2 -->
    <el-dialog
      v-model="tipShow2"
      title="温馨提示"
      width="500"
      :visible.sync="open2"
    >
      <span>确定要给当前所选用户批量补发奖品吗</span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelTip2">取消</el-button>
          <el-button type="primary" @click="confirmTip2"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import china from "../../assets/json/china.json";
import { getRewardRecord, getRewardReissue,getRewardLists } from "@/api/record/index";
import { getActivityReward, getAddressList } from "@/api/lottery/index";
export default {
  name: "Role",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //   是否弹出层
      open: false,
      open2: false,
      // 弹窗1展示
      tipShow1: false,
      // 弹窗2展示
      tipShow2: false,
      //   标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        provinceId: "",
        areaId: "",
        cityId: "",
        rewardId: "",
        status: "",
        time: "",
        start: "",
        end: "",
        activityCode: "",
      },
      // 表格数据
      recordList: [],
      // 表单校验
      rules: {},
      // 城市列表
      countryList: [],
      // 市列表
      cityList: [],
      //区列表
      areaList: [],
      // 奖品列表
      rewardList: [
        
      ],
      // 奖品状态
      rewardStatus: [
        { value: 0, value2: "NOT_EXCHANGE", label: "未兑奖" },
        {
          value: 1,
          value2: "EXCHANGED",
          label: "已兑奖",
        },
        {
          value: 2,
          value2: "EXPIRED",
          label: "已过期",
        },
        {
          value: -1,
          value2: "UNKNOWN",
          label: "未知",
        },
      ],
      // 补发奖品用户列表
      reissueList: [],
    };
  },
  created() {
    this.getProvinceCityArea({
      parentId: "",
    });
    this.getList({
      ...this.queryParams,
    });
    this.getActivityReward();
  },
  methods: {
    // 获取全国省
    getProvinceCityArea(params) {
      getAddressList(params).then((res) => {
        if (res.code == 0) {
          this.countryList = res.data.map((it) => {
            return {
              code: it.districtId,
              name: it.name,
            };
          });
        }
      });
    },
    // 获取provinceId下面的市
    getCity(params) {
      if (!params.parentId) {
        return;
      }
      getAddressList(params).then((res) => {
        if (res.code == 0) {
          this.cityList = res.data.map((it) => {
            return {
              code: it.districtId,
              name: it.name,
              parentId: it.parentId,
            };
          });
        }
      });
    },
    // 获取cityId下面的区
    getArea(params) {
      if (!params.parentId) {
        return;
      }
      getAddressList(params).then((res) => {
        if (res.code == 0) {
          this.areaList = res.data.map((it) => {
            return {
              code: it.districtId,
              name: it.name,
              parentId: it.parentId,
            };
          });
        }
      });
    },
    // 选取省 获取下面的市
    changeProvince(e) {
      this.getCity({
        parentId: e,
      });
      this.queryParams.cityId = "";
      this.cityList = [];
    },
    clearProvice() {
      // 全部清空省
      this.queryParams.provinceId = "";
      this.queryParams.cityId = "";
      this.queryParams.areaId = "";
      this.cityList = [];
      this.areaList = [];
    },
    // 选取市 获取下面的区
    changeCity(e) {
      this.getArea({
        parentId: e,
      });
      this.queryParams.areaId = "";
      this.areaList = [];
    },
    clearCity() {
      // 全部清空市
      this.areaList = [];
      this.queryParams.cityId = "";
      this.queryParams.areaId = "";
    },
    getList(params) {
      // this.countryList = china;
      getRewardRecord(params).then((res) => {
        this.recordList = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    // 搜索表格时间清空
    changeTime(e) {
      // console.log(e, "timechange");
      if (e != null) {
        this.queryParams.time = e;
        this.queryParams.start = this.formatDate(this.queryParams.time[0]);
        this.queryParams.end = this.formatDate(this.queryParams.time[1]);
      } else {
        this.queryParams.time = "";
        this.queryParams.start = "";
        this.queryParams.end = "";
      }
    },
    // 获取活动的奖品
    getActivityReward() {
      getRewardLists().then((res) => {
        if (res.code == 0) {
          // this.rewardList = res.data.records.map((it) => {
          //   return {
          //     label: it.title,
          //     options: it.rewardList.map((its) => {
          //       return {
          //         label: its.title,
          //         value: its.rewardId,
          //       };
          //     }),
          //   };
          // });
          this.rewardList = res.data.map((it)=>{
            return {
              label:it.title,
              value: it.hnRewardId,
            }
          })
        }
      });
    },
    // 格式化日期
    formatDate(date) {
      const year = date.getFullYear().toString();
      const month = ("0" + (date.getMonth() + 1)).slice(-2);
      const day = ("0" + date.getDate()).slice(-2);
      let hour = date.getHours();
      if (hour.toString().length < 2) {
        hour = "0" + hour;
      }
      let seconds = date.getSeconds();
      if (seconds.toString().length < 2) {
        seconds = "0" + seconds;
      }
      let minutes = date.getMinutes();
      if (minutes.toString().length < 2) {
        minutes = "0" + minutes;
      }
      return `${year}-${month}-${day} ${hour}:${minutes}:${seconds}`;
    },
    handleQuery() {
      if (this.queryParams.time && this.queryParams.time.length > 0) {
        this.queryParams.start = this.formatDate(this.queryParams.time[0]);
        this.queryParams.end = this.formatDate(this.queryParams.time[1]);
      }
      this.getList(this.queryParams);
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        provinceId: "",
        areaId: "",
        cityId: "",
        rewardId: "",
        status: "",
        time: "",
        start: "",
        end: "",
        activityCode: "",
      };
    },
    handleSelectionChange(e) {
      console.log(e, "select");
      this.reissueList = e;
    },
    handleAdd() {
      this.tipShow1 = true;
      this.open = true;
    },
    handleEdit(row) {},
    // 气泡确认
    confirmEvent(row) {
      console.log(row);
      getRewardReissue({ hitRecordIds: row.id }).then((res) => {
        if (res.code == 0) {
          this.$message.success("补发成功");
          this.getList({
            pageNum: 1,
            pageSize: 10,
            provinceId: "",
            rewardId: "",
            status: "",
            start: "",
            end: "",
            activityCode: "",
          });
        }
      });
    },
    cancelEvent() {},
    // 补发1确认
    confirmTip1() {
      // if (this.queryParams.status != "2") {
      //   this.$message.warning("请先选择已过期状态的记录");
      //   return;
      // }
      this.queryParams.status = 2;
      if (this.reissueList.length == 0) {
        this.$message.warning("请先选择要补发的记录");
        this.getList(this.queryParams);
        return;
      }
      this.tipShow2 = true;
      this.open2 = true;
    },
    cancelTip1() {
      this.tipShow1 = false;
      this.open = false;
    },
    // 补发2确认 this.reissueList里面的rewardId 多个,分割
    confirmTip2() {
      this.reissueList = this.reissueList.filter((it) => {
        return it.transferStatus == 0 || it.transferStatus == 2;
      });
      let ids = this.reissueList.map((item) => item.id).join(",");
      getRewardReissue({ hitRecordIds: ids }).then((res) => {
        if (res.code == 0) {
          this.$message.success("批量补发成功");
          this.getList({
            pageNum: 1,
            pageSize: 10,
            provinceId: "",
            areaId: "",
            cityId: "",
            rewardId: "",
            status: "",
            start: "",
            end: "",
            activityCode: "",
          });
          this.cancelTip1();
          this.cancelTip2();
        }
      });
    },
    cancelTip2() {
      this.tipShow2 = false;
      this.open2 = false;
    },
  },
};
</script>

<style scoped lang="scss"></style>
